<template>
  <el-upload
      ref="multipartFile"
      class="upload-demo"
      :action="url"
      :limit="1"
      accept=".zip"
      :on-exceed="handleExceed"
      :on-success="onSuccess"
      :auto-upload="false"
  >
    <template #trigger>
      <el-button type="primary">select file</el-button>
    </template>
    <el-button class="ml-3" type="success" @click="submitUpload"
    >upload to server
    </el-button
    >
    <template #tip>
      <div class="el-upload__tip" style="color: red">
        limit 1 file, new file will cover the old file
      </div>
    </template>
  </el-upload>
</template>

<script setup lang="ts">
import {ref} from 'vue'
import router from "@/router";
import {IUser} from "@/store/pojo/IUser";
import {UserStoreModule} from "@/store/modules/User";

const multipartFile = ref()
const projectId = sessionStorage.getItem("project.id");
const url = "/api/project/uploadResource?projectId="+ projectId;

const handleExceed = (files) => {
  multipartFile.value.clearFiles()
  multipartFile.value.handleStart(files[0])
}
const submitUpload = () => {
  multipartFile.value.submit()
}

const onSuccess = () => {
  const userInfo: IUser = UserStoreModule.userInfoFromSessionStorage;
  alert("将回到个人页面")
  router.push({
    path: "/user/publisher/workspace",
    query: {
      userId: userInfo.id
    }
  })
}

</script>